package com.hmy.cornpestdet.service;

import com.hmy.cornpestdet.mapper.FileUploadMapper;
import com.hmy.cornpestdet.pojo.Image;
import com.hmy.cornpestdet.pojo.User;
import com.hmy.cornpestdet.utils.CPDConst;
import com.hmy.cornpestdet.utils.Result;
import com.hmy.cornpestdet.vo.ImageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FileUploadService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private FileUploadMapper fileUploadMapper;

    public Result submit(List<ImageVo> imageVos){
        String uidStr = redisTemplate.opsForValue().get(CPDConst.CURRENT_USER_UID);
        if(uidStr == null){
            return Result.Error("当前Uid为空");
        }
        int uid = Integer.valueOf(uidStr);
        List<Image>images = new ArrayList<>();
        for(ImageVo imageVo : imageVos){
            Image image = new Image();
            image.setUid(uid);
            image.setImageName(imageVo.getImageName());
            image.setPredictResult(imageVo.getPredictResult());
            image.setAddDate(imageVo.getAddDate());
            images.add(image);
        }
        if(fileUploadMapper.submit(images) > 0){
            return Result.Success("插入图片数据成功",null);
        }
        return Result.Error("插入图片数据失败");
    }

}
